package cndw.core.dispatch;

import io.netty.channel.Channel;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 默认Session实现
 * 
 * @author zuojie.x
 */
class DefaultSession implements Session {
	private Channel channel;
	protected String playerId;
	private Map<Object, Object> attributeMap;
	
	DefaultSession(Channel channel) {
		this.channel = channel;
		this.attributeMap  = new ConcurrentHashMap<Object, Object>();
	}
	
	@Override
	public Channel getChannel() {
		return channel;
	}

	@Override
	public void setPlayerId(String playerId) {
		this.playerId = playerId;
	}
	
	@Override
	public String getPlayerId() {
		return playerId;
	}
	
	@Override
	public boolean isLogin() {
		return getPlayerId() != null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public <T> T setAttribute(Object key, T value) {
		return (T) attributeMap.put(key, value);
	}

	@SuppressWarnings("unchecked")
	@Override
	public <T> T getAttribute(Object key, T clazz) {
		return (T) attributeMap.get(key);
	}

	@SuppressWarnings("unchecked")
	@Override
	public <T> T removeAttribute(Object key) {
		return (T) attributeMap.remove(key);
	}
	
	@Override
	public void removeAllAttribute() {
		attributeMap.clear();
	}
}
